Operators in C++
Operators in C++ are symbols that represent computations or operations on variables and values. They are fundamental building blocks in C++ programming, and they can be classified into several categories based on their functionality.
Operators in C++ can be classified into 5 types:
1. Arithmetic Operators2. Assignment Operators
3. Relational Operators
4. Logical Operators
5. Bitwise Operators
1. Arithmetic Operators
Arithmetic operators in C++ are used to perform basic mathematical operations on numeric values.
using namespace std;
int main() {
int a = 10, b = 3;
int sum = a + b; // Addition
cout << "Sum: " << sum << endl;
int difference = a - b; // Subtraction
cout << "Difference: " << difference << endl;
int product = a * b; // Multiplication
cout << "Product: " << product << endl;
int quotient = a / b; // Division
cout << "Quotient: " << quotient << endl;
int remainder = a % b; // Modulus
cout << "Remainder: " << remainder << endl;
return 0;
}
2. Assignment Operators
The assignment operator in C++ is denoted by the equal sign (=). It is used to assign a value to a variable.
Additionally, there are compound assignment operators that combine an arithmetic operation with assignment. These operators include:
1. += (Add and assign)2. -= (Subtract and assign)
3. *= (Multiply and assign)
4. /= (Divide and assign)
5. %= (Modulus and assign)
using namespace std;
int main() {
int x = 5;
// Compound Assignment Operators
x += 3; // x = x + 3;
cout << "x += 3: " << x << endl; // Output: x += 3: 8
x -= 2; // x = x - 2;
cout << "x -= 2: " << x << endl; // Output: x -= 2: 6
x *= 4; // x = x * 4;
cout << "x *= 4: " << x << endl; // Output: x *= 4: 24
x /= 3; // x = x / 3;
cout << "x /= 3: " << x << endl; // Output: x /= 3: 8
x %= 5; // x = x % 5;
cout << "x %= 5: " << x << endl; // Output: x %= 5: 3
return 0;
}
3. Relational Operators
Relational operators in C++ are used to compare two values and determine the relationship between them. These operators return a Boolean result, indicating whether the specified relationship is true or false.
1. Equal to (==): Checks if two values are equal.2. Not equal to (!=): Checks if two values are not equal.
3. Greater than (>): Checks if the left operand is greater than the right operand.
4. Less than (<): Checks if the left operand is less than the right operand.
5. Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.
6. Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.
using namespace std;
int main() {
int a = 5, b = 10;
// Relational operators
if (a == b) {
cout << "a is equal to b" << endl;
}
if (a != b) {
cout << "a is not equal to b" << endl;
}
if (a > b) {
cout << "a is greater than b" << endl;
}
if (a < b) {
cout << "a is less than b" << endl;
}
if (a >= b) {
cout << "a is greater than or equal to b" << endl;
}
if (a <= b) {
cout << "a is less than or equal to b" << endl;
}
return 0;
}
4. Logical Operators
Logical operators in C++ are used to perform logical operations on Boolean values. These operators combine two or more Boolean expressions and produce a result based on the truth or falsity of those expressions.
1. Logical AND (&&): Returns true if both operands are true; otherwise, it returns false
2.Logical OR (||): Returns true if at least one of the operands is true; returns false if both are false
3. Logical NOT (!): Returns true if the operand is false; returns false if the operand is true. It negates the given condition.
Example of Logical Operator:
using namespace std;
int main() {
int a = 1, b = 0;
cout << "The value of a && b is " << (a && b) << endl;
cout << "The value of a || b is " << (a || b) << endl;
cout << "The value of !a is " << (!a) << endl;
return 0;
}
5. Bitwise Operators
Arithmetic operators in C++ are used to perform basic mathematical operations on numeric values.
using namespace std;
int main() {
int a = 22; // The Binary of 22 is 10110
int b = 18; //The BInary of 18 is 10010
cout << " The value of a & b is " << (a & b) << endl;
cout << " The value of a | b is " << (a | b) << endl;
cout << " The value of a ^ b is " << (a ^ b) << endl;
cout << " The value of ~a is " << (~a) << endl;
cout << " The value of a >> 2 is " << (a >> 2) << endl;
cout << " The value of a << 2 is " << (a << 2) << endl;
return 0;
}